class Utils(object):
    # EXTM3U
    # EXTINF:-1 ,华数_院线大片
    # http: // hls - ott - zhibo.wasu.tv / live / 357 / index.m3u8
    # iplay pro可以使用
    @staticmethod
    def __format_m3u_file(data):
        return '#EXTINF:-1,' + data['name'] + '\n' + data['url'] + '\n'

    # 根据key来去重
    @staticmethod
    def distinct(data, key):
        return list({v[key]: v for v in data}.values())

    @staticmethod
    def write_file(live_sources):
        with open("live_source.m3u", "w", encoding='utf8') as file:
            for item in live_sources:
                file.write(Utils.__format_m3u_file(item))
            file.close()

    @staticmethod
    def rtmp2flv(rtmpurl):
        if rtmpurl.startswith('rtmp'):
            return rtmpurl.replace('rtmp','http') +'.flv'
        return rtmpurl
